declare namespace g = "ddi:group:3_1";
declare namespace dc="ddi:dcelements:3_1";
declare namespace d="ddi:datacollection:3_1"; 
declare namespace dc2="http://purl.org/dc/elements/1.1/"; 
declare namespace s="ddi:studyunit:3_1"; 
declare namespace c="ddi:conceptualcomponent:3_1";
declare namespace r="ddi:reusable:3_1"; 
declare namespace a="ddi:archive:3_1"; 
declare namespace ddi="ddi:instance:3_1"; 
declare namespace l="ddi:logicalproduct:3_1";

import module namespace request="http://exist-db.org/xquery/request";

let $func := request:get-parameter("method", "")
let $id := request:get-parameter("id", "")
let $targetType := request:get-parameter("targetType", "")
let $targetID := request:get-parameter("targetID", "")
let $targetTypeParentID := request:get-parameter("targetTypeParentID","")

let $collection := "/db/ddi/data/ddi3/internal"

return        
    if($func = "GetItem") then
        if(empty($id)) then
            <ddi:Fragment/>
        else
            <ddi:Fragment>{collection($collection)//.[@id=$id]}</ddi:Fragment>
    else if($func = "FindItems") then
        if(empty($targetType)) then
            <ddi:Fragment/>
        else
            if($targetTypeParentID = "") then
                <ddi:FragmentInstance>
                {
                    for $item in collection($collection)//.[name() = $targetType]     
                         return element {"ddi:Fragment"}
                            {
                                element {$item/name()} 
                                { 
                                    $item/@id,
                                    $item/r:Label,
                                    $item/r:Description,
                                    $item/d:QuestionText                                
                                }
                            }
                }                    
                </ddi:FragmentInstance>
            else
                <ddi:FragmentInstance>
                {
                    for $item in collection($collection)//.[@id=$targetTypeParentID]/*[name() = $targetType]     
                        return element {"ddi:Fragment"}
                            {
                                element {$item/name()} 
                                { 
                                    $item/@id,
                                    $item/r:Label,
                                    $item/r:Description,
                                    $item/d:QuestionText                                
                                }
                            }
                }                       
                </ddi:FragmentInstance>
    else if($func = "GetReferencingItems") then 
        if(empty($targetID)) then
            <ddi:Fragment/>
        else
            <ddi:FragmentInstance>
            {
                for $item in collection($collection)//.[r:ID=$targetID]/..
                    return element {"ddi:Fragment"}
                        {
                            $item                            
                        }
            }                
            </ddi:FragmentInstance>
    else <ddi:Fragment/>